package org.spider.s8;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import jodd.http.Cookie;
import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

public class Spider {
	// private final String url_login =
	// "http://sisqq.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
	private final String url_login = "http://sisqq.com/member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=Lkuzl&inajax=1";
	// tid就是突破口
	private final String url_comment = "http://sisqq.com/forum.php?mod=post&action=reply&fid=96&tid=9238106&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1";
	private final String url_list = "http://sisqq.com/forum-96-1.html";

	MyBrowser mb = MyBrowser.newInstance();

	public void login() throws IOException {		
		mb.getBrowser().clearCookies();
		Digester md5 = new Digester(DigestAlgorithm.MD5);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("formhash", "c7e86d5f");

		params.put("referer", "http://sisqq.com/portal-2.html");
		params.put("clicaptcha", "");
		params.put("username", "onenpc");
		params.put("password", md5.digestHex("and9527"));
		params.put("questionid", "0");
		params.put("answer", "");
		HttpBrowser bResult = mb.send(HttpRequest.post(url_login).form(params));
		System.out.println("登陆:" + bResult.getPage());
		 HttpBrowser send = mb.send(HttpRequest.get("http://sisqq.com/portal-2.html"));
		 System.out.println("内容"+send.getHttpResponse().bodyText());
	}
	
	public void login3() {
		
	}
	protected PostMethod getPostMethod(String url, String myisLogin) {
		PostMethod post = new PostMethod(url);

		HttpMethodParams params = new HttpMethodParams();
		params.setContentCharset("GB2312");
		post.setParams(params);

		post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");

		if (myisLogin != null && !myisLogin.equals("")) {
			post.setRequestHeader("Cookie", "myisLogin=" + myisLogin);
		}
		
		
		post.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		post.setRequestHeader("Accept-Encoding","gzip, deflate");
		post.setRequestHeader("Accept-Language","zh-CN,zh;q=0.9");
		post.setRequestHeader("Cache-Control","max-age=0");
		post.setRequestHeader("Connection","keep-alive");
		post.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		post.setRequestHeader("Host","sisqq.com");
		post.setRequestHeader("Origin","http://sisqq.com");
		post.setRequestHeader("Referer","http://sisqq.com/portal-2.html");
		post.setRequestHeader("Upgrade-Insecure-Requests","1");
		post.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36");
		return post;
	}

	public void login2() {
		Digester md5 = new Digester(DigestAlgorithm.MD5);
		HttpRequest query = HttpRequest.post(url_login);
		query.form("username", "onenpc");
		query.form("password", md5.digestHex("and9527"));
		query.form("quickforward", "yes");
		query.form("handlekey", "ls");
		query.form("sectouchpoint", "0");
		query.form("referer", "portal.html");
		HttpResponse send = query.send();
		System.out.println(send.bodyText());
	}

	// 评论
	public void comment() {
		Map<String, String> params = new HashMap<String, String>();
		// message:我来了.....重新下载一次
		// posttime:1522367480
		// formhash:c24b0779
		// usesig:
		// subject:
		params.put("message", "我又来了.....");
		params.put("posttime", Convert.toStr(this.getSecondTimestampTwo(new Date())));
		params.put("formhash", "c24b0779");
		params.put("usesig", "");
		params.put("subject", "");
		Cookie c = new Cookie(
				"visid_incap_621422=lnM/JdZISxCihQ/X58OdcAJ5vVoAAAAAQUIPAAAAAACG0J+WwSNye7QUu7kouM+4; incap_ses_551_621422=cTAvKoNmGkfdiitESoylBwN5vVoAAAAACq4BYh9EqWByMwfXSrfLvA==; cus_cookie=14; nlbi_621422=npPmdFuFcQtQM3RryeqyHAAAAAAi3wU87mlbaSrJ0V+9VO5r; A8tI_2132_ignore_notice=1; A8tI_2132_lastvisit=1522363305; A8tI_2132_saltkey=wCcDJMzv; _ga=GA1.2.1686945084.1522338109; _gid=GA1.2.484669031.1522338109; A8tI_2132_auth=ffc2PJ4v3NjoR2px649HMAiHD%2FZBA7VP9d7y81eYBhsaBtlnu3Fjr9%2F%2FArNgSdCrtkjkUm44ScZHcWP1x%2FUjWprJUSjFWw; A8tI_2132_staticlogin=1; A8tI_2132_lastcheckfeed=90260119%7C1522367445; A8tI_2132_lip=8.8.8.8%2C1522367445; A8tI_2132_notification_unread_tips=1522366126; A8tI_2132_atarget=1; A8tI_2132_visitedfid=96; A8tI_2132_self_uid=90260119; A8tI_2132_smile=2D1; A8tI_2132_viewid=tid_9238106; A8tI_2132_credit_max_num=10; A8tI_2132_credit_remain_num=7; incap_ses_541_621422=LQ++dTFm+nttxQe5iAWCB8SGvVoAAAAAF2wYsmuG9n5J431+4ADmzA==; A8tI_2132_st_t=90260119%7C1522370473%7C8ac5234232da6fc30b4258a607b4fed5; A8tI_2132_forum_lastvisit=D_96_1522370473; A8tI_2132_st_p=90260119%7C1522370666%7Ce26e0c841ccea13ca5fcd30c7502e591; A8tI_2132_self_fid=-1; A8tI_2132_self_tid=-1; A8tI_2132_adv_gid=18; A8tI_2132_ulastactivity=1522370889%7C0; A8tI_2132_noticeTitle=1; A8tI_2132_self_unique_code=a56fc7d4-d878-f816-aaf4-4f6593ed5de6; __insp_wid=1484672786; __insp_slim=1522370890633; __insp_nv=true; __insp_targlpu=aHR0cDovL3Npc3FxLmNvbS9ob21lLnBocD9tb2Q9c3BhY2VjcCZhYz1jcmVkaXQmc2hvd2NyZWRpdD0x; __insp_targlpt=56ev5YiGIOadj_WQp1%2FmgKflkKdfc2V4OF%2FmnY%2FlkKfmnInkvaDmmKXmmpboirHlvIA%3D; __insp_norec_sess=true; A8tI_2132_lt_ad_1=1; A8tI_2132_lastact=1522370914%09forum.php%09ajax");
		Map<String, String> header = new HashMap<String, String>();
		header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		header.put("Accept-Encoding", "gzip, deflate");
		header.put("Accept-Language", "zh-CN,zh;q=0.9");
		header.put("Cache-Control", "max-age=0");
		header.put("Connection", "keep-alive");
		header.put("Content-Length", "110");
		header.put("Content-Type", "application/x-www-form-urlencoded");
		header.put("Cookie",
				"visid_incap_621422=lnM/JdZISxCihQ/X58OdcAJ5vVoAAAAAQUIPAAAAAACG0J+WwSNye7QUu7kouM+4; incap_ses_551_621422=cTAvKoNmGkfdiitESoylBwN5vVoAAAAACq4BYh9EqWByMwfXSrfLvA==; cus_cookie=14; nlbi_621422=npPmdFuFcQtQM3RryeqyHAAAAAAi3wU87mlbaSrJ0V+9VO5r; A8tI_2132_ignore_notice=1; A8tI_2132_lastvisit=1522363305; A8tI_2132_saltkey=wCcDJMzv; _ga=GA1.2.1686945084.1522338109; _gid=GA1.2.484669031.1522338109; A8tI_2132_auth=ffc2PJ4v3NjoR2px649HMAiHD%2FZBA7VP9d7y81eYBhsaBtlnu3Fjr9%2F%2FArNgSdCrtkjkUm44ScZHcWP1x%2FUjWprJUSjFWw; A8tI_2132_staticlogin=1; A8tI_2132_lastcheckfeed=90260119%7C1522367445; A8tI_2132_lip=8.8.8.8%2C1522367445; A8tI_2132_notification_unread_tips=1522366126; A8tI_2132_atarget=1; A8tI_2132_visitedfid=96; A8tI_2132_self_uid=90260119; A8tI_2132_smile=2D1; A8tI_2132_viewid=tid_9238106; A8tI_2132_credit_max_num=10; A8tI_2132_credit_remain_num=7; incap_ses_541_621422=LQ++dTFm+nttxQe5iAWCB8SGvVoAAAAAF2wYsmuG9n5J431+4ADmzA==; A8tI_2132_st_t=90260119%7C1522370473%7C8ac5234232da6fc30b4258a607b4fed5; A8tI_2132_forum_lastvisit=D_96_1522370473; A8tI_2132_st_p=90260119%7C1522370666%7Ce26e0c841ccea13ca5fcd30c7502e591; A8tI_2132_self_fid=-1; A8tI_2132_self_tid=-1; A8tI_2132_adv_gid=18; A8tI_2132_ulastactivity=1522370889%7C0; A8tI_2132_noticeTitle=1; A8tI_2132_self_unique_code=a56fc7d4-d878-f816-aaf4-4f6593ed5de6; __insp_wid=1484672786; __insp_slim=1522370890633; __insp_nv=true; __insp_targlpu=aHR0cDovL3Npc3FxLmNvbS9ob21lLnBocD9tb2Q9c3BhY2VjcCZhYz1jcmVkaXQmc2hvd2NyZWRpdD0x; __insp_targlpt=56ev5YiGIOadj_WQp1%2FmgKflkKdfc2V4OF%2FmnY%2FlkKfmnInkvaDmmKXmmpboirHlvIA%3D; __insp_norec_sess=true; A8tI_2132_lt_ad_1=1; A8tI_2132_lastact=1522370914%09forum.php%09ajax");
		header.put("Host", "sisqq.com");
		header.put("Origin", "http://sisqq.com");
		header.put("Referer", "http://sisqq.com/thread-9238106-2-1.html");
		header.put("Upgrade-Insecure-Requests", "1");
		header.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36");

		HttpResponse send = HttpRequest.post(url_comment).query(params).header(header).cookies(c).send();
		System.out.println(send.bodyText());
	}

	private int getSecondTimestampTwo(Date date) {
		if (null == date) {
			return 0;
		}
		String timestamp = String.valueOf(date.getTime() / 1000);
		return Integer.valueOf(timestamp);
	}
}
